package actions;

import java.util.ArrayList;

import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.struts2.ServletActionContext;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import searcher.AskSearcher;

import com.opensymphony.xwork2.ActionContext;

import entity.Ask;

public class SearchOne extends BaseAction {
	private String lowerBound;
	private String upperBound;
	private String city;
	private String room1;
	private String room2;
	private String room3;
	private String details;
	private String page;
	private String ifUseXML;
	private ArrayList<Ask> rlt;

	public String execute() throws Exception {

		String lbStr = this.getLowerBound();
		String ubStr = this.getUpperBound();
		String detStr = this.getDetails();
		String xml = this.getIfUseXML();
		if (lbStr != null && lbStr.length() == 0) {
			lbStr = "-1";
		}
		if (ubStr != null && ubStr.length() == 0) {
			ubStr = "-1";
		}
		if (detStr != null && detStr.length() == 0) {
			detStr = null;
		}
		AskSearcher temp = new AskSearcher(this.getCity(),
				Integer.parseInt(lbStr), Integer.parseInt(ubStr),
				Integer.parseInt(this.getRoom1()), Integer.parseInt(this
						.getRoom2()), Integer.parseInt(this.getRoom3()), detStr);
		int pageCount = temp.preAskSearch();

		String curPage = this.getPage();
		int pagenum = 1;
		if (null != curPage) {
			pagenum = Integer.parseInt(curPage) > 1 ? Integer.parseInt(curPage)
					: 1;
			pagenum = pagenum <= pageCount ? pagenum : pageCount;

		}
		if ((xml != null && xml.compareTo("否") == 0) || xml == null || xml.length() == 0) {
			ActionContext.getContext().getSession().put("page", pagenum);
			rlt = temp.getResultatPage(pagenum);
			ActionContext.getContext().put("rlts", rlt);
			ActionContext.getContext().put("lowerBound", this.getLowerBound());
			ActionContext.getContext().put("upperBound", this.getUpperBound());
			ActionContext.getContext().put("city", this.getCity());
			ActionContext.getContext().put("room1", this.getRoom1());
			ActionContext.getContext().put("room2", this.getRoom2());
			ActionContext.getContext().put("room3", this.getRoom3());
			ActionContext.getContext().put("details", this.getDetails());
			return "input";

		} else if (xml.compareTo("是") == 0) {
			rlt = temp.getResultatPage(pagenum);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("text/xml;charset=UTF-8");
			response.setHeader("Cache-Control", "no-cache");
			return SUCCESS;
		}
		return curPage;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getLowerBound() {
		return lowerBound;
	}

	public void setLowerBound(String lowerBound) {
		this.lowerBound = lowerBound;
	}

	public String getUpperBound() {
		return upperBound;
	}

	public void setUpperBound(String upperBound) {
		this.upperBound = upperBound;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getRoom1() {
		return room1;
	}

	public void setRoom1(String room1) {
		this.room1 = room1;
	}

	public String getRoom2() {
		return room2;
	}

	public ArrayList<Ask> getRlt() {
		return rlt;
	}

	public void setRlt(ArrayList<Ask> rlt) {
		this.rlt = rlt;
	}

	public void setRoom2(String room2) {
		this.room2 = room2;
	}

	public String getRoom3() {
		return room3;
	}

	public void setRoom3(String room3) {
		this.room3 = room3;
	}

	public String getDetails() {
		return details;
	}

	public void setDetails(String details) {
		this.details = details;
	}

	public String getIfUseXML() {
		return ifUseXML;
	}

	public void setIfUseXML(String ifUseXML) {
		this.ifUseXML = ifUseXML;
	}

}
